﻿using hyjiacan.py4n;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Developer.Service
{
    public class StringService
    {
        /// <summary>
        /// 汉字转拼音
        /// </summary>
        /// <param name="key">汉字</param>
        /// <returns></returns>
        public static string HanToPinyin(string key)
        {
            string multi = "FIRST";
            string cmd = "0";
            //WITH_TONE_MARK：声调标志
            //WITH_TONE_NUMBER：声调数值
            //WITHOUT_TONE：无声调
            string toneType = "WITHOUT_TONE";
            //WITH_U_UNICODE：ü
            //WITH_V：v
            //WITH_U_AND_COLON：u:
            string vType = "WITH_V";
            //UPPERCASE：大写
            //LOWERCASE：小写
            //CAPITALIZE_FIRST_LETTER：首字母大写
            string caseType = "CAPITALIZE_FIRST_LETTER";

            string result = string.Empty;
            try { 
                // 请求参数不为空才处理
                if (!string.IsNullOrEmpty(key))
                {
                    if (cmd == "0")
                    {
                        #region // 汉字转拼音
                        // 解析从客户端来的输出格式设置
                        PinyinOutputFormat format = new PinyinOutputFormat(toneType, caseType, vType);

                        foreach (char ch in key)
                        {
                            // 是汉字才处理
                            if (PinyinUtil.IsHanzi(ch))
                            {
                                // 是否只取第一个拼音
                                if (multi.Equals("first", StringComparison.OrdinalIgnoreCase))
                                {
                                    string py = Pinyin4Net.GetUniqueOrFirstPinyinWithFormat(ch, format);
                                    // 拼音间追加空字符串（空格），这里如果是多间字，拼音可能不准确
                                    result += py + "";
                                }
                                else
                                {
                                    string[] py = Pinyin4Net.GetPinyinWithFormat(ch, format);
                                    result += "(" + string.Join(",", py) + ") ";
                                }
                            }
                            else
                            {// 不是汉字直接追加
                                result += ch.ToString();
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        #region // 拼音查汉字
                        string[] hanzi = Pinyin4Net.GetHanzi(key, true);
                        result = string.Join(",", hanzi);
                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }
            return result;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
